1 | 'use strict';
|
2 |
|
3 | var Path = require('path');
|
4 | var Should = require('should');
|
5 | var SuperTest = require('supertest');
|
6 | var IFNode = require('../');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var app = IFNode({
|
13 | project_folder: Path.resolve(__dirname, '../examples/controllers'),
|
14 | alias: 'cntllrs'
|
15 | }).load();
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var app_map = IFNode({
|
22 | project_folder: Path.resolve(__dirname, '../examples/controller_map'),
|
23 | alias: 'cntllrs_map'
|
24 | }).load();
|
25 |
|
26 | describe('Controllers', function() {
|
27 | describe('Controllers loading', function() {
|
28 | it('should throw doubling names error', function() {
|
29 | (function() {
|
30 | IFNode({
|
31 | project_folder: Path.resolve(__dirname, '../examples/controllers_with_doubling_error'),
|
32 | alias: 'controllers_with_doubling_error'
|
33 | }).load();
|
34 | }).should.throw();
|
35 | });
|
36 |
|
37 | it('should load controllers from custom folder', function() {
|
38 | var app = IFNode({
|
39 | project_folder: Path.resolve(__dirname, '../examples/controllers'),
|
40 | alias: 'controllers-from-custom-folder',
|
41 | environment: 'controllers-from-custom-folder'
|
42 | }).load();
|
43 |
|
44 | Should.exist(app.controllers.from_custom_folder);
|
45 | });
|
46 | });
|
47 |
|
48 | describe('app.Controller(options?: Object)', function() {
|
49 | it('default options', function() {
|
50 | app.controllers.main.root.should.be.equal('/');
|
51 | app.controllers.main.name.should.be.equal('main');
|
52 |
|
53 | app.controllers['api/~'].root.should.be.equal('/api/');
|
54 | app.controllers['api/~'].name.should.be.equal('api/~');
|
55 |
|
56 | app.controllers['api/v1/user'].root.should.be.equal('/api/v1/user/');
|
57 | app.controllers['api/v1/user'].name.should.be.equal('api/v1/user');
|
58 | });
|
59 | });
|
60 |
|
61 | describe('Controller instance', function() {
|
62 | describe('.param(name: string, expression: function)', function() {
|
63 | it('should get correct params', function() {
|
64 | (function() {
|
65 | app.controllers.main.param('abc', /test/);
|
66 | }).should.throw();
|
67 | (function() {
|
68 | app.controllers.main.param(/bad-name/, function() {});
|
69 | }).should.throw();
|
70 | });
|
71 |
|
72 | it('should create param checker', function() {
|
73 | app.controllers.main.param('param-checker', function() {
|
74 | });
|
75 | })
|
76 | });
|
77 |
|
78 | it('.before(...callbacks: Array<routeHandler>)', function(done) {
|
79 | SuperTest(app.listener)
|
80 | .get('/check_before')
|
81 | .expect({ check_before: 'invoked' }, done);
|
82 | });
|
83 |
|
84 | describe('options', function() {
|
85 | describe('.map', function() {
|
86 | it('should throw error', function() {
|
87 | (function () {
|
88 | IFNode({
|
89 | project_folder: Path.resolve(__dirname, '../examples/controller_map_with_error'),
|
90 | alias: 'cntllrs_map_with_error'
|
91 | }).load();
|
92 | }).should.throw();
|
93 | });
|
94 |
|
95 | it('get /', function(done) {
|
96 | SuperTest(app_map.listener)
|
97 | .get('/')
|
98 | .expect('simple route', done);
|
99 | });
|
100 | it('get /with-error', function(done) {
|
101 | SuperTest(app_map.listener)
|
102 | .get('/with-error')
|
103 | .expect(500, 'GET with error', done);
|
104 | });
|
105 | it('get /:param', function(done) {
|
106 | SuperTest(app_map.listener)
|
107 | .get('/1')
|
108 | .expect({ id: 1, name: 'ilfroloff' }, done);
|
109 | });
|
110 | it('post /', function(done) {
|
111 | SuperTest(app_map.listener)
|
112 | .post('/')
|
113 | .expect({ permanent: 'yes' }, done);
|
114 | });
|
115 | it('put /:id', function(done) {
|
116 | SuperTest(app_map.listener)
|
117 | .put('/1')
|
118 | .expect('updated', done);
|
119 | });
|
120 | it('delete /:id', function(done) {
|
121 | SuperTest(app_map.listener)
|
122 | .delete('/1')
|
123 | .expect({ custom: 'yes' }, done);
|
124 | });
|
125 |
|
126 | it('should prevent add route', function() {
|
127 | (function() {
|
128 | app_map.controllers.main.get(function() {});
|
129 | }).should.throw();
|
130 | });
|
131 | });
|
132 |
|
133 | describe('.ajax', function() {
|
134 | it('should accept AJAX request', function(done) {
|
135 | SuperTest(app.listener)
|
136 | .get('/api/ajax-tests/accept-ajax')
|
137 | .set('X-Requested-With', 'XMLHttpRequest')
|
138 | .expect({
|
139 | accept_ajax: true
|
140 | }, done);
|
141 | });
|
142 | it('should except non-AJAX request', function(done) {
|
143 | SuperTest(app.listener)
|
144 | .get('/api/ajax-tests/accept-ajax')
|
145 | .expect(400, done);
|
146 | });
|
147 | it('should accept non-AJAX request', function(done) {
|
148 | SuperTest(app.listener)
|
149 | .get('/api/ajax-tests/except-ajax')
|
150 | .expect({
|
151 | except_ajax: true
|
152 | }, done);
|
153 | });
|
154 | it('should except AJAX request', function(done) {
|
155 | SuperTest(app.listener)
|
156 | .get('/api/ajax-tests/except-ajax')
|
157 | .set('X-Requested-With', 'XMLHttpRequest')
|
158 | .expect(400, done);
|
159 | });
|
160 | });
|
161 |
|
162 | it('permanent', function(done) {
|
163 | SuperTest(app.listener)
|
164 | .get('/check_permanent_options')
|
165 | .expect({ permanent: 'yes' }, done);
|
166 | });
|
167 | it('custom', function(done) {
|
168 | SuperTest(app.listener)
|
169 | .get('/check_custom_options')
|
170 | .expect({ custom: 'yes' }, done);
|
171 | });
|
172 | });
|
173 |
|
174 | describe('.method(method: string|Array, route?: string, options?: Object, handler: function)', function() {
|
175 | describe('map of methods', function() {
|
176 | it('default', function(done) {
|
177 | SuperTest(app.listener)
|
178 | .get('/')
|
179 | .expect({
|
180 | default: true,
|
181 | default_special_options: true
|
182 | }, done);
|
183 | });
|
184 |
|
185 | it('get', function(done) {
|
186 | SuperTest(app.listener)
|
187 | .get('/10')
|
188 | .expect({ id: 10 }, done);
|
189 | });
|
190 | it('post', function(done) {
|
191 | SuperTest(app.listener)
|
192 | .post('/11')
|
193 | .expect({ id: 11 }, done);
|
194 | });
|
195 | it('put', function(done) {
|
196 | SuperTest(app.listener)
|
197 | .put('/a')
|
198 | .expect({ id: 0 }, done);
|
199 | });
|
200 | it('delete', function(done) {
|
201 | SuperTest(app.listener)
|
202 | .delete('/method-delete')
|
203 | .expect('works', done);
|
204 | });
|
205 | it('del', function(done) {
|
206 | SuperTest(app.listener)
|
207 | .delete('/method-del')
|
208 | .expect('works', done);
|
209 | });
|
210 | it('del, delete', function(done) {
|
211 | SuperTest(app.listener)
|
212 | .delete('/methods-delete')
|
213 | .expect('works', done);
|
214 | });
|
215 | });
|
216 |
|
217 | it('should catch internal error and send to .error()', function(done) {
|
218 | SuperTest(app.listener)
|
219 | .get('/api/v1/user')
|
220 | .expect({
|
221 | handled_by_api_v1_user: 'Unexpected error on handler'
|
222 | }, done);
|
223 | });
|
224 |
|
225 | it('should returns status 404', function(done) {
|
226 | SuperTest(app.listener)
|
227 | .delete('/api/v1/user/non-exists-route')
|
228 | .expect(404, done);
|
229 | });
|
230 |
|
231 | it('should jump to next controller', function(done) {
|
232 | var url = '/api/jump/to/next';
|
233 |
|
234 | SuperTest(app.listener)
|
235 | .get(url)
|
236 | .expect({
|
237 | echo: url
|
238 | }, done)
|
239 | })
|
240 | });
|
241 |
|
242 | describe('.error(handler: function)', function() {
|
243 | it('fire', function(done) {
|
244 | SuperTest(app.listener)
|
245 | .get('/error/fire')
|
246 | .expect({ error: 'fire' }, done);
|
247 | });
|
248 |
|
249 | it('should throw unhandled error', function(done) {
|
250 | var app = IFNode({
|
251 | project_folder: Path.resolve(__dirname, '../examples/controllers_with_unhandled_error'),
|
252 | alias: 'controllers_with_unhandled_error'
|
253 | }).load();
|
254 |
|
255 | SuperTest(app.listener)
|
256 | .get('/')
|
257 | .expect(500, done);
|
258 | });
|
259 | });
|
260 |
|
261 | describe('.compile()', function() {
|
262 | Should.not.exist(app.controllers.main.compile());
|
263 | });
|
264 | });
|
265 |
|
266 | describe('Custom request/response options', function() {
|
267 | it('should throw error by redefining', function(done) {
|
268 | |
269 |
|
270 |
|
271 |
|
272 | var app = IFNode({
|
273 | project_folder: Path.resolve(__dirname, '../examples/controller_middleware_with_error'),
|
274 | alias: 'controller_middleware_with_error',
|
275 | environment: 'local'
|
276 | }).load();
|
277 |
|
278 | SuperTest(app.listener)
|
279 | .get('/')
|
280 | .expect(500, done);
|
281 | });
|
282 |
|
283 | it('response.ok(data: IFResponseData)', function(done) {
|
284 | SuperTest(app.listener)
|
285 | .get('/custom-response-methods/ok')
|
286 | .expect(200, done);
|
287 | });
|
288 |
|
289 | it('response.fail(data: IFResponseData)', function(done) {
|
290 | SuperTest(app.listener)
|
291 | .get('/custom-response-methods/fail')
|
292 | .expect(400, done);
|
293 | });
|
294 |
|
295 | it('response.bad_request(data: IFResponseData)', function(done) {
|
296 | SuperTest(app.listener)
|
297 | .get('/custom-response-methods/bad_request')
|
298 | .expect(400, done);
|
299 | });
|
300 |
|
301 | it('response.badRequest(data: IFResponseData)', function(done) {
|
302 | SuperTest(app.listener)
|
303 | .get('/custom-response-methods/badRequest')
|
304 | .expect(400, 'non-empty-data', done);
|
305 | });
|
306 |
|
307 | it('response.unauthorized(data: IFResponseData)', function(done) {
|
308 | SuperTest(app.listener)
|
309 | .get('/custom-response-methods/unauthorized')
|
310 | .expect(401, done);
|
311 | });
|
312 |
|
313 | it('response.forbidden(data: IFResponseData)', function(done) {
|
314 | SuperTest(app.listener)
|
315 | .get('/custom-response-methods/forbidden')
|
316 | .expect(403, done);
|
317 | });
|
318 |
|
319 | it('response.not_found(data: IFResponseData)', function(done) {
|
320 | SuperTest(app.listener)
|
321 | .get('/custom-response-methods/not_found')
|
322 | .expect(404, done);
|
323 | });
|
324 |
|
325 | it('response.notFound(data: IFResponseData)', function(done) {
|
326 | SuperTest(app.listener)
|
327 | .get('/custom-response-methods/notFound')
|
328 | .expect(404, done);
|
329 | });
|
330 |
|
331 | it('response.err(data: IFResponseData)', function(done) {
|
332 | SuperTest(app.listener)
|
333 | .get('/custom-response-methods/err')
|
334 | .expect(500, done);
|
335 | });
|
336 |
|
337 | it('response.error(data: IFResponseData)', function(done) {
|
338 | SuperTest(app.listener)
|
339 | .get('/custom-response-methods/error')
|
340 | .expect(500, 'non-empty-data', done);
|
341 | });
|
342 |
|
343 | it('should throw error about redefining', function(done) {
|
344 | SuperTest(app.listener)
|
345 | .get('/custom-response-methods/error')
|
346 | .expect(500, done);
|
347 | })
|
348 | });
|
349 | });
|